//
//  ViewController.m
//  GCD常用函数
//
//  Created by 李煜 on 2024/3/20.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//  [self delay];
//  [self apply];
  [self cutFiles];
}
#pragma mark - 快速迭代函数
- (void)forDemo {
  for (int i = 0 ; i<100; i++) {
    NSLog(@"%d---%@",i,[NSThread currentThread]);
  }
}
- (void)apply {


  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  /**
   第一个参数:遍历的次数
   第二个参数:队列(必须并发队列.填主队列会死锁,填串行队列和for循环没啥区别)
   第三个参数:index索引
   */
  dispatch_apply(10, queue, ^(size_t index) {
    NSLog(@"%zu---%@",index,[NSThread currentThread]);
  });

}

///文件剪切
///需求:桌面有from和to两个文件夹,将from文件夹下的文件剪切到to文件夹下
- (void)cutFiles {
  NSString *from = @"/Users/liyu/Desktop/from";
  NSString *to = @"/Users/liyu/Desktop/to";

  NSArray *names = [[NSFileManager defaultManager]subpathsAtPath:from];


  NSInteger count = names.count;
  dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i) {
    NSString *fileName = names[i];
    NSString *fromFullPath = [from stringByAppendingPathComponent:fileName];
    NSString *toFullPath = [to stringByAppendingPathComponent:fileName];

    [[NSFileManager defaultManager] moveItemAtPath:fromFullPath toPath:toFullPath error:nil];
  });
}
@end
